package chat.activities;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;

import java.util.ArrayList;
import java.util.List;

import chat.adapter.FriendRequstAdapter;
import chat.network.http.GetFriendRequest;
import inter.Delegate;
import inter.ResponseStatus;

/**
 * 新的好用请求界面
 * Created by Administrator on 2016/3/29.
 */
public class NewFriendRequest extends UIHandler.BaseActivity {

    ListView lv_newfriendrequst;

    FriendRequstAdapter friendRequstAdapter = new FriendRequstAdapter();

    List<GetFriendRequest.NewFriends> newFriends = new ArrayList<GetFriendRequest.NewFriends>();
    {
        friendRequstAdapter.setNewFriends(newFriends);
    }

    Delegate GetFriendRequestResponese  = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetFriendRequest.ResponseNewFriends responseNewFriends = gson.fromJson(response, GetFriendRequest.ResponseNewFriends.class);

            if (responseNewFriends.getStatus().equals(ResponseStatus.SUCCESS))
            {
                Toast.makeText(MyApplication.share(),"获取新的好用请求",Toast.LENGTH_SHORT).show();
                newFriends.clear();
                newFriends.addAll(responseNewFriends.getNewFriends());
                friendRequstAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };
    GetFriendRequest getFriendRequest = new GetFriendRequest();
    {
        getFriendRequest.setDelegate(GetFriendRequestResponese);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newfriendrequest);

        initview();
        initdata();
    }

    private void initdata() {
        getFriendRequest.setUserID(MyApplication.getOwnUser().getUser_ID());
        getFriendRequest.get();
    }

    //
    private void initview() {
        lv_newfriendrequst = (ListView) findViewById(R.id.id_lv_newfriendrequest);
        lv_newfriendrequst.setAdapter(friendRequstAdapter);

    }
}
